package com.service.impl;

import com.constant.Constant;
import com.dao.PraiseMapper;
import com.entity.Praise;
import com.entity.User;
import com.exception.ServiceException;
import com.service.PraiseService;
import com.util.AjaxResult;
import com.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Service
public class PraiseServiceImpl implements PraiseService {
    @Autowired
    PraiseMapper praiseMapper;
    //插入点赞记录  李奇洲
    @Override
    public AjaxResult insertPraise(HttpSession session, Integer commentId) {
        System.out.println("已经进入点赞方法");
        System.out.println("Session="+session);
        AjaxResult ajaxResult = new AjaxResult();
        Praise praise = new Praise();
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        try{
            praise.setUser_id(user.getId());
            praise.setCreate_date(new Date());
            praise.setComment_id(commentId);
            System.out.println("praise="+praise);
            Praise praiseTemp = praiseMapper.selectPraiseByCommentIdAndUserId(commentId, user.getId());
            if(praiseTemp==null){
                int i = praiseMapper.insertPraise(praise);
                System.out.println("插入返回值"+i);
                ajaxResult.setSuccess(i==1?true:false);
                }else{
                int i1 = praiseMapper.deletePraise(commentId, user.getId());
                System.out.println(i1);
            }
        }catch (ServiceException ex){
            ex.printStackTrace();
            System.out.println("异常！！！");
        }
        return ajaxResult;
    }

    //计算点赞总数 返回给前端
    @Override
    public CommentVo findPrasieCountByUidAndCommentID(Integer commentId) {
        CommentVo commentVo = praiseMapper.selectPraiseByUserIdAndCommentId(commentId);
        return commentVo;
    }
}
